{template 'common/header'}
  <ul class="nav nav-tabs">    
    <li><a href="{php echo $this->createWebUrl('setCard');}">微信卡券</a></li>
    <li><a href="{php echo $this->createWebUrl('setMiicard');}">系统优惠券</a></li>
    <li class="active"><a href="{php echo $this->createWebUrl('setmembercard');}">会员卡设置</a></li>
    <li><a href="{php echo $this->createWebUrl('setcardoff');}">卡券核销</a></li>
  </ul>
<div class="panel we7-panel ng-scope" >
	<div class="panel-heading">会员卡设置</div>
	<div class="panel-body we7-padding">
		<div class="form-horizontal">
			<div class="form-group">
				<label class="col-sm-2 control-label">会员卡状态</label>
				<div class="col-sm-8">
					<div id="open-mcard" class="miiswitch" data-on="warning">
					    <input type="checkbox" checked />
					    <input type="hidden" name="mcard_status"  value="{if $o['status']=='-1'}-1{else}1{/if}">
					</div>						
				</div>
		 	</div>

			<div class="form-group">
				<label class="col-sm-2 control-label">是否开启激活收费</label>
				<div class="col-sm-8">
					<div id="open-activation" class="miiswitch" data-on="warning">
					    <input type="checkbox" checked />
					    <input type="hidden" name="ismcardactivation"  value="{if $o['activation']=='-1'}-1{else}1{/if}">
					</div>
				    <div class="input-group" id="activation_money_input">
				      <div class="input-group-addon">激活收费</div>
				      <input type="number" class="form-control" id="activation_money" placeholder="" value="{$o['activation_money']}">
				      <div class="input-group-addon">元</div>
				    </div>						
				</div>
		 	</div>

			<div class="form-group">
				<label class="col-sm-2 control-label">是否开启充值优惠</label>
				<div class="col-sm-8">
					<div id="open-recharge-discount" class="miiswitch" data-on="warning">
					    <input type="checkbox" checked />
					    <input type="hidden" name="recharge_discount"  value="{if $o['recharge_discount']=='-1'}-1{else}1{/if}">
					</div>
					<div id="recharge-discount-box">
					    <ul id="recharge-discount-ul">
					    {if $discount_info}
					    {loop $discount_info $key $d}
						<li class="input-group" id="recharge-discount-list" lid="{php echo $key+1}" style="margin-top: 2px;">
						      <div class="input-group-addon">充值满</div>
						      <input type="number" class="form-control" id="condition_money{php echo $key+1}" placeholder="" value="{$d['condition_money']}">
						      <div class="input-group-addon">元</div>
						      <div class="input-group-addon">送</div>
						      <input type="number" class="form-control" id="give_money{php echo $key+1}" placeholder="" value="{$d['give_money']}">
						      <div class="input-group-addon">元</div>
						      <div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;"" onclick="del_discount({php echo $key+1})">删除</div>
					  	  </li>
					  	  {/loop}		
					  	  {else}
					  	  {/if}			  	  
					    </ul>
					    <div id="add_discount" class="addmenu">增加一个充值优惠</div>		
					</div>    			    						
				</div>
		 	</div>

			<div class="form-group">
				<label class="col-sm-2 control-label">默认会员等级设置</label>
				<div class="col-sm-8">
				    <div class="input-group" id="activation_money_input">
				      <div class="input-group-addon">等级名称</div>
				      	<input type="text" class="form-control" id="default_mcard_level_name" placeholder="" value="{$o['default_mcard_level_name']}">
				    </div>
				    <span class="help-block">&nbsp;&nbsp;如贵宾VIP</span>
				    <div class="input-group" id="activation_money_input">
				      <div class="input-group-addon">会员消费折扣</div>
				      	<input type="number" class="form-control" id="default_mcard_level_discount" placeholder="" value="{$o['default_mcard_level_discount']}">
				      <div class="input-group-addon">折</div>
				    </div>
				    <span class="help-block">&nbsp;&nbsp;数字格式,1-10之间 </span>
				</div>
		 	</div>

		 	<div class="form-group">
		 		<label class="col-sm-2 control-label">开启会员等级</label>
		 	</div>
			<div class="form-group">
				<div class="col-sm-10">
					<div id="open-mcard-level" class="miiswitch" data-on="warning">
					    <input type="checkbox" checked />
					    <input type="hidden" name="ismemberlevel"  value="{if $o['islevel']=='-1'}-1{else}1{/if}">
					</div>
					<div id="mcard-level-box">
						<ul id="mcard-level-ul">		
							{if $level_info}
							{loop $level_info $key $l}
							<li class="input-group" id="mcard-level-list" lid="{php echo $key+1}">
							  <div class="input-group-addon">等级</div>
						      	<select style="width: 100px;" type="text" class="form-control" id="mcard_level_level{php echo $key+1}" >
						      		{loop levelNumber() $k $n}
						      		<option value="{$n}" {if $n==$l['mcard_level_level']}selected="selected"{else}{/if}> {$n} </option>
						      		{/loop}
						      	</select>	
						      <div class="input-group-addon">等级名称</div>
						      	<input type="text" class="form-control" id="mcard_level_name{php echo $key+1}" placeholder="" value="{$l['mcard_level_name']}">
						      <div class="input-group-addon">折扣</div>
						      	<input type="number" class="form-control" id="mcard_level_discount{php echo $key+1}" placeholder="" value="{$l['mcard_level_discount']}">
						      <div class="input-group-addon">折</div>
						      <div class="input-group-addon">升级条件,消费满</div>
						      	<input type="number" class="form-control" id="mcard_level_condition{php echo $key+1}" placeholder="" value="{$l['mcard_level_condition']}">
						      <div class="input-group-addon">元</div>
						      <div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;"" onclick="del_level({php echo $key+1})">删除</div>
						    </li> 
						    {/loop}
						    {else}
						    {/if}
					    </ul>	
					    <div id="addmcardlevel" class="addmenu">增加一个会员等级</div>
					</div>
				</div>			
		 	</div>


			<div class="form-group">
				<label class="col-sm-2 control-label">使用说明事项</label>
				<div class="col-sm-8">
					<textarea class="form-control" id="description" name="description"  rows="3">{$o['description']}</textarea>
					<span class="help-block">&nbsp;&nbsp;换行请以 || 分割 </span>
				</div>
			</div>			 	


			<button type="button" id="save_" class="btn btn-info" style="display: block; margin-left: auto;margin-right: auto;">提交保存</button>
		</div>
	</div>
</div>		 	  
<style type="text/css">
.right-content{padding: 0;} 
.ishide{display: none;}
.addmenu{
    width: 150px;
    height:auto;
    background-color: #ff5d38;
    color: #FFFFFF;
    border-radius: 2px;
    border:0;
    display: block;
    padding: 2px 5px;
    box-sizing: border-box;
    box-shadow: 0 2px 2px 0 rgba(255,93,56, 0.14), 0 3px 1px -2px rgba(255,93,56, 0.2), 0 1px 5px 0 rgba(255,93,56, 0.12);    
    line-height: 35px;  
    outline: none;  
    margin-left: auto;
    margin-right: auto;
    margin-top: 10px;
    text-align: center;
    cursor:default;
}
.addmenu:hover{
    box-shadow: 0 14px 26px -12px rgba(255,93,56, 0.42), 0 4px 23px 0px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(255,93,56, 0.2);
}
</style>
<link rel="stylesheet" href="{MODULE_URL}public/css/bootstrapSwitch.css">
<script src="{MODULE_URL}public/js/switch.js"></script> 
<link rel="stylesheet" href="{MODULE_URL}public/css/Toast.css">
<script src="{MODULE_URL}public/js/Toast.js"></script>  
<script type="text/javascript">
	$('#open-activation').on('switch-change', function (e, data) {
    var $el = $(data.el)
      , value = data.value;
    if(value){
    	$("input[name='ismcardactivation']").val('1');
    	if($('#activation_money_input').hasClass("ishide")){
    		$("#activation_money_input").removeClass("ishide");
    	}
    	return false;
    }else{
    	$("input[name='ismcardactivation']").val('-1');
    	if(!$('#activation_money_input').hasClass("ishide")){
    		$("#activation_money_input").addClass("ishide");
    	}
    	return false;    	
    	}
	});


	$('#open-mcard').on('switch-change', function (e, data) {
    var $el = $(data.el)
      , value = data.value;
    console.log($el,value);
    if(value){
    	$("input[name='mcard_status']").val('1');
    	return false;
    }else{
    	$("input[name='mcard_status']").val('-1');
    	$('#open-activation').bootstrapSwitch('setState', false); 
		$('#open-mcard-level').bootstrapSwitch('setState', false); 
		$('#open-recharge-discount').bootstrapSwitch('setState', false);    	
    	return false;    	
    	}
	});	
	//开启会员等级
	$('#open-mcard-level').on('switch-change', function (e, data) {
	    var $el = $(data.el)	
	      , value = data.value;
	    console.log($el,value);
	    if(value){
	    	$("input[name='ismemberlevel']").val('1');
	    	if($("#mcard-level-box").hasClass('ishide')){
	    	   $('#mcard-level-box').removeClass("ishide");
	    	}
	    	return false;
	    }else{
	    	$("input[name='ismemberlevel']").val('-1');
	    	if(!$("#mcard-level-box").hasClass('ishide')){
	    	   $('#mcard-level-box').addClass("ishide");
	    	}	    	
	    	return false;    	
	    }
	});
	//充值优惠
	$('#open-recharge-discount').on('switch-change', function(e,data){
	    var $el = $(data.el)	
	      , value = data.value;
	    console.log($el,value);
	    if(value){
	    	$("input[name='recharge_discount']").val('1');
	    	if($('#recharge-discount-box').hasClass("ishide")){
	    		$("#recharge-discount-box").removeClass("ishide");
	    	}	    	
	    	return false;
	    }else{
	    	$("input[name='recharge_discount']").val('-1');
	    	if(!$('#recharge-discount-box').hasClass("ishide")){
	    		$("#recharge-discount-box").addClass("ishide");
	    	}	    	
	    	return false;    	
	    }		
	})
	//	

    $(document).ready(function(){
    	{if $o['status']=='-1'}
	    	$('#open-mcard').bootstrapSwitch('setState', false); 
	    	$('#open-activation').bootstrapSwitch('setState', false); 
			$('#open-mcard-level').bootstrapSwitch('setState', false); 
			$('#open-recharge-discount').bootstrapSwitch('setState', false);  
		{else}
			{if $o['activation']=='-1'}
				$('#open-activation').bootstrapSwitch('setState', false);
			{else}
				$('#open-activation').bootstrapSwitch('setState', true);
			{/if}
			{if $o['islevel']=='-1'}
				$('#open-mcard-level').bootstrapSwitch('setState', false); 
			{else}
				$('#open-mcard-level').bootstrapSwitch('setState', true); 
			{/if}
			{if $o['recharge_discount']=='-1'}
				$('#open-recharge-discount').bootstrapSwitch('setState', false);  
			{else}
				$('#open-recharge-discount').bootstrapSwitch('setState', true);  
			{/if}
		{/if}
	    //增加充值优惠
	    $("#add_discount").click(function(){
		    var _len = $("#recharge-discount-ul li").length+1;
		    $("#recharge-discount-ul").append(  
		    	'<li class="input-group" id="recharge-discount-list" lid="'+_len+'" style="margin-top: 2px;">'+
			      '<div class="input-group-addon">充值满</div>'+
			      '<input type="number" class="form-control" id="condition_money'+_len+'" value="" placeholder="">'+
			      '<div class="input-group-addon">元</div>'+
			      '<div class="input-group-addon">送</div>'+
			      '<input type="number" class="form-control" id="give_money'+_len+'" placeholder="" value="">'+
			      '<div class="input-group-addon">元</div>'+
			      '<div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;" onclick=\"del_discount('+_len+')\">删除</div>'+
			    '</li>'              
		        );
		    return false;
	    });

	    $("#addmcardlevel").click(function(){
	    	var _len = $("#mcard-level-ul li").length+1;
	    	$("#mcard-level-ul").append(
				'<li class="input-group" id="mcard-level-list" lid="'+_len+'" style="margin-top: 2px;">'+
				'<div class="input-group-addon">等级</div>'+
				'<select style="width: 100px;" type="text" class="form-control" id="mcard_level_level'+_len+'" >'+
	      		{loop levelNumber() $key $n}
	      		'<option value="{$n}"> {$n} </option>'+
	      		{/loop}
				'</select>'+
			    '<div class="input-group-addon">等级名称</div>'+
			    '<input type="text" class="form-control" id="mcard_level_name'+_len+'" placeholder="" value="">'+
			    '<div class="input-group-addon">折扣</div>'+
			    '<input type="number" class="form-control" id="mcard_level_discount'+_len+'" placeholder="" value="">'+
			    '<div class="input-group-addon">折</div>'+
			    '<div class="input-group-addon">升级条件,消费满</div>'+
			    '<input type="number" class="form-control" id="mcard_level_condition'+_len+'" placeholder="" value="">'+
			    '<div class="input-group-addon">元</div>'+
			    '<div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;" onclick=\"del_level('+_len+')\">删除</div>'+
			    '</li>'    		
	    	);
	    	return false;
	    });
	});
	//删除充值优惠
	var del_discount =function(index)
	{
	    var _len = $("#recharge-discount-ul li").length+1;
	    $("#recharge-discount-list[lid='"+index+"']").remove();//删除充值优惠当前行
	    for(var i=index+1,j=_len;i<j;i++)
	    {
	        var condition_money = $("#condition_money"+i).val();
	        var give_money = $("#give_money"+i).val();
	        $("#recharge-discount-list[lid=\'"+i+"\']").replaceWith(
				'<li class="input-group" id="recharge-discount-list" lid="'+(i-1)+'" style="margin-top: 2px;">'+
			      '<div class="input-group-addon">充值满</div>'+
			      '<input type="number" class="form-control" id="condition_money'+(i-1)+'" value="'+condition_money+'" placeholder="">'+
			      '<div class="input-group-addon">元</div>'+
			      '<div class="input-group-addon">送</div>'+
			      '<input type="number" class="form-control" id="give_money'+(i-1)+'" placeholder="" value="'+give_money+'">'+
			      '<div class="input-group-addon">元</div>'+
			      '<div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;" onclick=\"del_discount('+(i-1)+')\">删除</div>'+
			    '</li>'
	        );
	    }
	    return false;
	}

	var del_level = function(index)	
	{
		var _len = $("#mcard-level-ul li").length+1;
		$("#mcard-level-list[lid='"+index+"']").remove();//删除等级当行
		for (var i = index+1,j=_len;i<j;i++) 
		{	var mcard_level_level = $.trim($('#mcard_level_level'+i+' option:selected').val());
			var mcard_level_name = $("#mcard_level_name"+i).val();
			var mcard_level_discount = $("#mcard_level_discount"+i).val();
			var mcard_level_condition = $("#mcard_level_condition"+i).val();
			$("#mcard-level-list[lid=\'"+i+"\']").replaceWith(
				'<li class="input-group" id="mcard-level-list" lid="'+(i-1)+'" style="margin-top: 2px;">'+
				'<div class="input-group-addon">等级</div>'+
				'<select style="width: 100px;" type="text" class="form-control" id="mcard_level_level'+(i-1)+'" >'+
				'<option value="'+mcard_level_level+'" selected="selected" > '+mcard_level_level+' </option>'+
	      		{loop levelNumber() $key $n}
	      		'<option value="{$n}"> {$n} </option>'+
	      		{/loop}
				'</select>'+				
			    '<div class="input-group-addon">等级名称</div>'+
			    '<input type="text" class="form-control" id="mcard_level_name'+(i-1)+'" placeholder="" value="'+mcard_level_name +'">'+
			    '<div class="input-group-addon">折扣</div>'+
			    '<input type="number" class="form-control" id="mcard_level_discount'+(i-1)+'" placeholder="" value="'+mcard_level_discount+'">'+
			    '<div class="input-group-addon">折</div>'+
			    '<div class="input-group-addon">升级条件,消费满</div>'+
			    '<input type="number" class="form-control" id="mcard_level_condition'+(i-1)+'" placeholder="" value="'+mcard_level_condition+'">'+
			    '<div class="input-group-addon">元</div>'+
			    '<div class="input-group-addon" style="margin-left: 4px; background: #ff5d38; color: #fff;cursor:default;" onclick=\"del_level('+(i-1)+')\">删除</div>'+
			    '</li>'
			);
		}
	}
	//保存
	$("#save_").click(function(){
		//处理数组值
		var status = $.trim($('input[name="mcard_status"]').val());
		var activation = $.trim($('input[name="ismcardactivation"]').val());
		var activation_money =  $.trim($('#activation_money').val());
		var recharge_discount = $.trim($('input[name="recharge_discount"]').val());//是否开启折扣	
		if(!d_info(recharge_discount)&&recharge_discount=='1'){
			return false;
		}
		var recharge_discount_info = d_info(recharge_discount)?d_info(recharge_discount):'';
		var default_mcard_level_name = $.trim($('#default_mcard_level_name').val());
		var default_mcard_level_discount = $.trim($('#default_mcard_level_discount').val());
		var islevel = $.trim($('input[name="ismemberlevel"]').val());	
		if(!l_info(islevel)&&islevel=='1'){
			return false;
		}
		var member_level_info = l_info(islevel)?l_info(islevel):'';
		var description = $.trim($('#description').val());
		if(status=='1'){
			if(activation==''){
				mqToast({type:1,context:"请设置激活是否收费"});
				return false;
			}
			if(activation=='1'&&activation_money==''){
				mqToast({type:1,context:"请设置激活收费"});
				return false;
			}
			if(isNaN(activation_money)){
				mqToast({type:1,context:"激活收费应为数字"});
				return false;
			}
			if(default_mcard_level_name==''){
				mqToast({type:1,context:"请设置默认会员等级名称"});
				return false;
			}
			if(default_mcard_level_discount==''){
				mqToast({type:1,context:"请设置默认会员等级折扣"});
				return false;
			}
			if(description==''){
				mqToast({type:1,context:"请填写使用说明事项"});
				return false;				
			}
		}
		$.ajax({
			url:"{php echo $this->createWebUrl('save_membercardinfo');}&version_id="+"{$_GPC['version_id']}",
			data:{status:status,activation:activation,activation_money:activation_money,recharge_discount:recharge_discount,recharge_discount_info:recharge_discount_info,default_mcard_level_name:default_mcard_level_name,default_mcard_level_discount:default_mcard_level_discount,islevel:islevel,member_level_info:member_level_info,description:description},
			dataType:"json",
			type:"post",
			success:function(res){
				if(res.status=='1'){
					mqToast({type:1,context:res.msg});
					return false;					
				}else{
					mqToast({type:1,context:res.msg});
					return false;					
				}
			}
		})	
	});
	//获取会员等级组值
	var l_info = function(islevel){
		var level_len = $("#mcard-level-ul li").length;
		var levelarr = [];
		var open = islevel
		if(level_len>0){
			for (var i = 0; i < level_len; i++) {
				var j= i+1;
				var mcard_level_level = $.trim($('#mcard_level_level'+j+' option:selected').val());
				var mcard_level_name = $.trim($('#mcard_level_name'+j).val());
				var mcard_level_discount = $.trim($("#mcard_level_discount"+j).val());
				var mcard_level_condition = $.trim($("#mcard_level_condition"+j).val());
				if(mcard_level_name==""||mcard_level_discount==''||mcard_level_condition==""||isNaN(mcard_level_discount)|| isNaN(mcard_level_condition)){
					if(open=='1'){
						mqToast({type:2,context:"请补全会员等级第 "+j+" 条数据"});
					}
					return false;
				}
				levelarr[i] = mcard_level_level+'||'+mcard_level_name+'||'+mcard_level_discount+'||'+mcard_level_condition;
			}
			return levelarr.join('oOo');
		}else{
			if(open=='1'){
				mqToast({type:2,context:"请添加会员等级"});
			}			
			return false;
		}		
	}

	var d_info = function(isopen){
		var _len = $("#recharge-discount-ul li").length;
		var arr = [];
		var open = isopen;
		if(_len>0){
			for (var i = 0; i < _len; i++) {
				var j = i+1;
				var condition_money = $.trim($('#condition_money'+j).val());
				var give_money = $.trim($('#give_money'+j).val());
				if(condition_money==''|| give_money==''|| isNaN(condition_money)||isNaN(give_money))
				{
					if(open=='1'){
						mqToast({type:2,context:"请补全充值优惠第 "+j+" 条数据"});
					}
					return false;					
				}
				arr[i] = condition_money+'||'+give_money;
			}
			return arr.join('oOo');
		}else{
			if(open=='1'){
				mqToast({type:2,context:"请添加充值优惠"});
			}
			return false;
		}
	}

</script>

{template 'common/footer'} 